package com.museum.service.impl.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.museum.base.BaseApiService;
import com.museum.base.BaseResponse;
import com.museum.entity.DO.*;
import com.museum.entity.DTO.admin.QueryRole;
import com.museum.entity.DTO.role.RoleAdd;
import com.museum.entity.DTO.role.RoleShow;
import com.museum.mapper.*;
import com.museum.service.impl.PermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description:
 * @ClassName museum
 * @Author: 王瑞文
 * @Date: 2020/12/26 17:37
 */
@Service
public class AdminRoleService extends BaseApiService<JSONObject> {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 获取所有角色信息
     *
     * @param current
     * @param limit
     * @return
     */
    public BaseResponse<JSONObject> getAllRole(Integer current, Integer limit, QueryRole queryRole) {

        Integer areaNumber = queryRole.getAreaNumber();
        String roleName = queryRole.getRoleName();
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("update_time");
        wrapper.ne("role_name", "超级管理员");
        if (null != roleName) wrapper.like("role_name", roleName);
        if (null != areaNumber && areaNumber != 0) wrapper.eq("area_number", areaNumber);
        List<RoleShow> roleShows = new ArrayList<>();
        Page page = new Page(current, limit);
        roleMapper.selectPage(page, wrapper);
        //获得所有的角色(不包括系统级别)
        List<Role> roles = page.getRecords();

        List<String> remove = new ArrayList<>();
        List<Integer> canUsePermissionIDs = permissionMapper.getCanUsePermissionIDs();
        for (Integer canUsePermissionID : canUsePermissionIDs) {
            if (permissionMapper.getParentIdByPermissionIds(canUsePermissionID).size() != 0)
                remove.add(String.valueOf(canUsePermissionID));
        }


        //将每一个权限递归 找到他所有的权限  列出权限树
        for (Role role : roles) {
            Area area = areaMapper.selectOne(new QueryWrapper<Area>().eq("area_number", role.getAreaNumber()));
            RoleShow roleShow = new RoleShow();
            BeanUtils.copyProperties(role, roleShow);
            if (area != null)
                roleShow.setAreaName(area.getAreaName());
            else {
                roleShow.setAreaName("所有地域共享");
            }
            Integer roleId = roleShow.getRoleId();
            List<String> strings = rolePermissionMapper.selectPermissionIdsByRoleIdForUpdate(roleId, role.getAreaNumber());

            List<String> list = new ArrayList<>();
            if (strings.size() != 0)
                list = remove(strings, remove);

            roleShow.setPermissionIds(list);
            roleShows.add(roleShow);
        }

        JSONObject object = new JSONObject();
        object.put("roles", roleShows);
        object.put("total", page.getTotal());
        return setResultSuccess(object);
    }

    public static List<String> remove(List<String> strings, List<String> remove) {
        for (String s : remove) {
            if (strings.contains(s))
                strings.remove(s);
        }
        return strings;
    }

    /**
     * 添加角色
     *
     * @param roleAdd
     * @return
     */
    public BaseResponse<JSONObject> addRole(RoleAdd roleAdd) {

        Role role = new Role();
        BeanUtils.copyProperties(roleAdd, role);
        roleMapper.insert(role);

        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(role.getRoleId());
        rolePermission.setPermissionId(999);
        rolePermissionMapper.insert(rolePermission);
        String[] permissionId = roleAdd.getPermissionId();
        List<Integer> tree = new ArrayList<>();
        for (String s : permissionId) {
            tree = permissionService.findIDFather(tree, Integer.parseInt(s));
        }
        Set<Integer> set = new HashSet<>();
        for (Integer integer : tree) {
            set.add(integer);
        }
        addPermission(role.getRoleId(), set);
        return setResultSuccess("添加成功");
    }

    //role_permission 表添加数据
    public void addPermission(Integer roleId, Set<Integer> permissions) {
        for (Integer permission : permissions) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permission);
            rolePermissionMapper.insert(rolePermission);
        }
    }

}
